<?php
namespace Admin\Controller;

//商户分享
use Common\Controller\AdminbaseController;

class MerchantshareController extends AdminbaseController
{
    protected $article_model;

    public function _initialize()
    {
        parent::_initialize();
        $this->article_model = D("Common/Article");
    }

    //商户分享列表
    public function index()
    {
        /**搜索条件**/
        $article_title = I('request.article_title');
        if($article_title){
            $where['article_title'] = array('like',"%$article_title%");
        }
        $where['article_type'] = 'merchant_share';

        $count=$this->article_model->where($where)->count();
        $page = $this->page($count, 10);
        $list = $this->article_model
            ->where($where)
            ->order("id DESC")
            ->limit($page->firstRow, $page->listRows)
            ->select();
        $this->assign("page", $page->show('Admin'));
        $merchant = D("Common/Merchant")->field('id,name')->order("id DESC")->select();
        $this->assign('merchant',$merchant);
        $this->assign('list',$list);
        $this->display();
    }

    //商户分享新增
    public function add()
    {
        $list = D("Common/Merchant")->field('id,name')->order("id DESC")->select();
        $this->assign('list',$list);
        $this->display();
    }

    // 商户分享添加提交
    public function add_post(){
        if (IS_POST) {

            if ($this->article_model->create() !== false) {
                $date = $_POST;
                $res = $this->article_model->where(array('merchant_id'=>$date['merchant_id']))->find();
                if ($res){
                    $this->error("该商户已经有文章！");
                }
                /*当前管理员name*/
                $date['article_type'] = 'merchant_share';
                $date['create_user'] = get_current_admin_name();
                $date['create_time'] = get_admin_time();
                $result = $this->article_model->add($date);
                if ($result !== false) {
                    $this->success("添加成功！");
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error($this->article_model->getError());
            }
        }
    }

    //商户分享编辑
    public function edit()
    {
        $id = I('get.id',0,'intval');
        $share = $this->article_model->where(array('id' => $id))->find();
        $list = D("Common/Merchant")->field('id,name')->order("id DESC")->select();
        $this->assign('list',$list);
        $this->assign('share',$share);
        $this->display();

    }


    //商户分享编辑提交
    public function edit_post()
    {
        if (IS_POST) {
            if ($this->article_model->create() !== false) {
                $date = $_POST;
                /*当前管理员name*/
                $date['update_user'] = get_current_admin_name();
                $date['update_time'] = get_admin_time();
                $result = $this->article_model->save($date);
                if ($result !== false) {
                    $this->success("编辑成功！");
                } else {
                    $this->error("编辑失败！");
                }
            } else {
                $this->error($this->article_model->getError());
            }
        }

    }

    // 商户分享删除
    public function delete(){
        $id = I('get.id',0,'intval');
        if ($this->article_model->delete($id)!==false) {
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }


}